package com.yoomiito.app.ui.search

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.bean.GoodsInfo
import com.yoomiito.app.net.YmApi

class SearchResultPresenter(appConfig: AppConfig?) : BaseActPresenter<SearchResultActivity?>(appConfig) {
    fun searchGoods(key: String?, sort: String?, hasCoupon: Int, page: Int) {
        YmApi.getYmService()
                .searchGoods(key, sort, hasCoupon, page)
                .compose(v?.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<List<GoodsInfo?>>(handleException()) {

                    override fun onFail(error: ApiException) {
                        v?.dismissLoading()
                    }

                    override fun onSuccess(t: List<GoodsInfo?>?) {
                        v?.setData(t)
                    }
                })
    }
}